<template>
	<om-addpage :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit" :showHistoryBtn="false" :saveBtnText="'确认'">
		<om-autoform ref="autoform" :initConfigData="initConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		errorModal,
		ignoreHtml,
		getUrl,
		confirmModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildFormData({
				startNodeTimeCache: "askLeaveTemplate-startNodeTimeId-cache",
				initValue:{
					
				},
				initConfigData: [{
					key: "title0",
					editors: [
						{
							checkedit: null,
							editReadonly: null,
							isfull: null,
							max: "",
							min: "",
							data: [],
							nameField: "leaveTypeName",
							required: true,
							title: "请假类别",
							prompt: "请选择",
							type: "selectbox",
							valueField: "leaveTypeId",
							onSelect: "leaveTypeFunc"
						},{
							checkedit: null,
							editReadonly: null,
							isfull: null,
							max: "",
							min: "0.5",
							nameField: "leaveDate",
							prompt: "请输入0.5的倍数",
							required: true,
							title: "请假天数",
							type: "numberbox",
							valueField: "leaveDate",
							onSelect: "changeDate"
						},{
							checkedit: null,
							editReadonly: null,
							isfull: null,
							max: "",
							min: "",
							nameField: "startDate",
							prompt: "请选择",
							required: true,
							title: "开始日期",
							type: "newdatebox",
							valueField: "startDate"
						},
						{
							checkedit: null,
							editReadonly: null,
							isfull: null,
							data: [],
							max: "",
							min: "",
							nameField: "startNodeTimeName",
							required: true,
							title: "开始时间",
							prompt: "请选择",
							type: "linkbox",
							valueField: "startNodeTimeId"
						}
					]
				}]
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
		methods: {
			leaveTypeFunc(data){
				let _that = this
				let formdata = _that.$refs.autoform.formdata
				formdata.leaveTypeId = data.id
				formdata.leaveTypeName = data.dicname
				formdata.ratio = data.ratio
				formdata.defaultId = data.defaultId
			},
			changeDate(data){
				let _that = this
				let formdata = _that.$refs.autoform.formdata
				data = Number(data);
				if (data % 0.5 > 0) {
				    formdata.leaveDate = data - data % 0.5
				}
				if(data < 0.5){
					formdata.leaveDate = 0.5
				}
			},
			formSubmit() {
				let _that = this
				uni.setStorageSync('askLeaveTemplate',JSON.stringify(_that.$refs.autoform.formdata));
				uni.navigateBack()
			},
			initLoad(options) {
				let _that = this
				let pro1 = _that.om.request('askLeaveDict/getList')
				Promise.all([pro1]).then(res => {
					changeEditor("title0", "leaveTypeId", "data", configSelectData(res[0], "id", "dicname"), _that.initConfigData)
					
					changeEditor("title0","startNodeTimeId",{
						link: "../../public/search/search",
						linkParam: {
							cachename: _that.startNodeTimeCache,
							initload: true,
							other: JSON.stringify({
								userId: _that.userId
							}),
							searchbar: false,
							namefield: "name",
							notefield:"node",
							extrafield:"startTime",
							url: "ttimeTable/getStartNodeList",
						},
					},null,_that.initConfigData)
					
					_that.$nextTick(function(){
						let syncData = uni.getStorageSync('askLeaveTemplate');
						if(syncData){
							let obj = JSON.parse(syncData);
							_that.$refs.autoform.formdata.leaveTypeId = obj.leaveTypeId;
							_that.$refs.autoform.formdata.leaveTypeName = obj.leaveTypeName;
							_that.$refs.autoform.formdata.leaveDate = obj.leaveDate;
							_that.$refs.autoform.formdata.startDate = obj.startDate;
							_that.$refs.autoform.formdata.startNodeTimeName = obj.startNodeTimeName;
							_that.$refs.autoform.formdata.startNodeTimeId = obj.startNodeTimeId;
							_that.$refs.autoform.formdata.ratio = obj.ratio;
							_that.$refs.autoform.formdata.defaultId = obj.defaultId;
							_that.$refs.autoform.formdata.ed = obj.ed;
							_that.$refs.autoform.formdata.ed1 = obj.ed1;
							_that.$nextTick(function() {
								_that.$refs.autoform.initSelectIndex('title0',"leaveTypeId")
							})
						}
					})
				})
			}
		},
		onLoad(options) {
			let _that = this
			uni.setNavigationBarTitle({
				title: '按天请假'
			});
			
			_that.busCode = options.busCode
			// Object.assign(_that.initValue, _that.formdata)
			_that.initLoad(options);
		},
		onShow() {
			let _that = this
			if (_that.$refs.autoform) {
				uni.getStorage({
					key: _that.startNodeTimeCache,
					success: (res) => {
						let d = res.data
						let formdata = _that.$refs.autoform.formdata
						formdata.startNodeTimeId = d.startTime
						formdata.startNodeTimeName = d.startTime
						formdata.ed = d.endTime
						formdata.ed1 = d.endTime1
						uni.removeStorage({
							key: _that.startNodeTimeCache
						})
					}
				})
			}
		}
	}
</script>

<style>
</style>
